<?php

namespace App\Http\Controllers;

use App\Models\ArticleCategory;
use App\Models\ArticlePhoto;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use \DB;
use App\Http\Requests\NewsRequest;
use App\Models\Article;

class NewsController extends CommonController
{
    /**
     * 显示资讯列表
     *
     * @return Response
     */
    public function getList()
    {
        $lists = Article::orderBy('created_at', 'desc')->orderBy('recommend', 'asc')->with('articleCategory')->paginate(10);
        $categoryList = ArticleCategory::get();

        return view('news.list')->with("lists", $lists)->with("categoryList", $categoryList)->with("actions", $this->actions);
    }

    /**
     *执行一个搜索
     */
    public function search(Request $request)
    {
        $data = $request->input();
        $lists = Article::orderBy('recommend', 'asc')->orderBy('created_at', 'desc')->with('articleCategory');
        if (isset($data['subject']) && $data['subject'] != '') {
            $lists->where('subject','like', '%'.$data['subject'].'%');
        }

        if (isset($data['source']) && $data['source'] != '') {
            $lists->where('source','like', '%'.$data['source'].'%');
        }

        if (isset($data['category_id']) && $data['category_id'] != '') {
            $lists->where('category_id', $data['category_id']);
        }
        $lists = $lists->paginate(10);
        $categoryList = ArticleCategory::get();

//        dd($lists);

        return view('news.search')->with('lists', $lists)->with("actions", $this->actions)
            ->with("categoryList", $categoryList)->with("subject", $data['subject'])
            ->with("source", $data['source'])->with("category_id", $data['category_id']);
    }

    /**
     * 获取资讯详情编辑页面
     */
    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        $list = Article::where('id', $id)->first();
        $cats = ArticleCategory::get();

        return view('news.edit')->with("list", $list)->with("cats", $cats);
    }

    /**
     * 编辑一条资讯
     */
    public function postEdit(NewsRequest $request)
    {
        $data = $request->input();
        unset($data['formType'], $data['_token'], $data['myfile']);

        if ($data['pubdate']) {
            $data['pubdate'] = strtotime($data['pubdate']);
        }

        if ($data['thumb'] == '') {
            unset($data['thumb']);
        }

        Article::where('id', '=', $data['id'])->update($data);

        return AjaxCallbackMessage('资讯编辑成功', true, url('news/list'));
    }

    /**
     * 获取创建资讯的页面
     */
    public function getCreate()
    {
        $cats = ArticleCategory::get();

        return view('news.create')->with("cats", $cats);
    }

    /**
     * 创建一个资讯
     */
    public function postCreate(NewsRequest $request)
    {
        $data = $request->input();

        if (!$data['pubdate']) {
            $data['pubdate'] = time();
        } else {
            $data['pubdate'] = strtotime($data['pubdate']);
        }
        unset($data['formType'], $data['_token']);

        $result = Article::create($data);

        if (is_object($result)) {
            return AjaxCallbackMessage('资讯添加成功', true, url('news/list'));
        } else {
            return AjaxCallbackMessage('资讯添加失败', false);
        }
    }

    /**
     * 显示单个资讯详情
     */
    public function getInfo(Request $request)
    {
        $id = $request->input('id');

        $list = Article::where('id', $id)->first();
        $cats = ArticleCategory::get();

        return view('news.info')->with("list", $list)->with("cats", $cats);
    }

    /**
     * 获取单条资讯图片管理页面
     */
    public function getPhotos(Request $request)
    {
        $data = $request->input();
        $photos = ArticlePhoto::where('article_id', $data['id'])->paginate(10);

        return view('articlephoto.photos')->with("lists", $photos)->with("actions", $this->actions)->with("article_id", $data['id']);
    }

    /**
     * 获取增加图片的界面
     *
     */
    public function getAddPhoto(Request $request)
    {
        $data = $request->input();
        $article_id = $data['article_id'];

        return view('articlephoto.create')->with('article_id', $article_id);
    }

    /**
     * 执行图片地址添加
     */
    public function postAddPhoto(Request $request)
    {
        $data = $request->input();

        $article_id = $data['article_id'];
        unset($data['_token'], $data['formType'], $data['article_id']);
        foreach ($data as $k => $v) {
            $v != '' && ArticlePhoto::create(['article_id' => $article_id, 'image' => $v]);
        }

        return AjaxCallbackMessage('图片新增成功', true);
    }

    /**
     * 删除一张图片
     */
    public function postDelPhoto(Request $request)
    {
        $data = $request->input();
        ArticlePhoto::where('id', $data['pid'])->delete();

        return AjaxCallbackMessage('图片删除成功', true, url('news/photos?id=' . $data['article_id']));
    }

    /**
     * 删除一条资讯
     */
    public function postDelete(Request $request)
    {
        $data = $request->input();

//        dd($data);
        Article::where('id', $data['nid'])->delete();
        ArticlePhoto::where('article_id', $data['nid'])->delete();

        return AjaxCallbackMessage('资讯删除', true, url('news/list'));
    }
}
